﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using MyBlog.DataAccess;
using MyBlog.Model;
using MyBlog.Common;
using AutoMapper;
using MyBlog.BusinessInterface;
using MyBlog.DataEntity;
using MyBlog.DataAccessInterface;
namespace MyBlog.Business
{
    public class BusinessNotice : IBusinessNotice
    {
        IDALNotice _dalNotice;
        IUnitOfWork _unitWork;
        public BusinessNotice(IDALNotice dalNotice, IUnitOfWork unitWork)
        {
            _dalNotice = dalNotice;
            _unitWork = unitWork;
        }
        public void Add(NoticeModel model)
        {
            var dataEntity = Mapper.Map<Tbl_Notice>(model);
            _dalNotice.Add(dataEntity);
            _unitWork.Commit();
            model.ID = dataEntity.ID;
        }

        public void Update(NoticeModel model)
        {
            var dataEntity = Mapper.Map<Tbl_Notice>(model);
            _dalNotice.Update(dataEntity);
            _unitWork.Commit();
        }
        public void AddRange(IEnumerable<NoticeModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_Notice>>(models);
            _dalNotice.AddRange(dataEntitys);
            _unitWork.Commit();
        }

        public void DeleteRange(IEnumerable<NoticeModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_Notice>>(models);
            _dalNotice.DeleteRange(dataEntitys);
            _unitWork.Commit();
        }

        public List<NoticeModel> GetAll(bool isTracking = true)
        {
            var entities = _dalNotice.GetAll(isTracking);
            return Mapper.Map<List<NoticeModel>>(entities);
        }

        public void Delete(params object[] primaryKey)
        {
            _dalNotice.Delete(primaryKey);
            _unitWork.Commit();
        }

        public NoticeModel Get(params object[] primaryKey)
        {
            var dataEntity = _dalNotice.Get(primaryKey);
            return Mapper.Map<NoticeModel>(dataEntity);
        }

        public void Delete(NoticeModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Notice>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            _dalNotice.Delete(where);
            _unitWork.Commit();
        }

        public NoticeModel Get(NoticeModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Notice>();
            if (model.ID != 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            var dataEntity = _dalNotice.Get(where);
            return Mapper.Map<NoticeModel>(dataEntity);
        }

        public List<NoticeModel> GetAll(NoticeModel model, bool isTracking = true)
        {
            var where = LinqExpressionBuilder.True<Tbl_Notice>();
            var entities = _dalNotice.GetAll(where, isTracking);
            return Mapper.Map<List<NoticeModel>>(entities);
        }


        public List<NoticeModel> GetAll(NoticeModel model, int page, int size, out int total, bool isTracking = true)
        {
            throw new NotImplementedException();
        }
    }
}
